/**
 * @project heima-leadnews
 * @author MECHREVO
 * @date 2024/1/21 19:42:29
 * @version 1.0
 */
package com.heima.schedule.fegin;

import com.heima.fegin.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.schedule.dto.TaskDto;
import com.heima.schedule.service.TaskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ScheduleClient implements IScheduleClient {
    @Autowired
    private TaskInfoService taskInfoService;
    /**
     * 添加任务
     *
     * @param taskDto 任务对象
     * @return 任务id
     */
    @PostMapping("/api/v1/schedule/addTask")
    public void addTask(TaskDto taskDto) {
        taskInfoService.addTask(taskDto);

    }

    /**
     * 按照类型和优先级来拉取任务
     *
     * @param taskType
     * @param priority
     * @return
     */
    @PostMapping("/api/v1/schedule/pullTask/{taskType}/{priority}")
    public ResponseResult pullTask(Integer taskType, Integer priority) {
        TaskDto taskDto = taskInfoService.pullTask(taskType, priority);
        if(taskDto!=null){
            byte[] parameters = taskDto.getParameters();
            String newIdStr = new String(parameters);
            return ResponseResult.okResult(newIdStr);
        }else {
         return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }
}
